package com;


import com.fz.entity.Student;
import com.fz.mapper.StudentMapper;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.sql.DataSource;
import java.util.List;
import java.util.Properties;

/**
 * Created by bai on 2017/6/23.
 */
public class Demo {
    protected SqlSessionFactory sf;
    protected SqlSession session;
    protected StudentMapper stu;

    @Before
    public void init() throws Exception {
        Properties p = new Properties();
        p.setProperty("driver","com.mysql.jdbc.driver");
        p.setProperty("url","jdbc:mysql://localhost:3306/student?useSSL=true&useUnicode=true&characterEncoding=UTF8");
        p.setProperty("username","root");
        p.setProperty("password","root");
        p.setProperty("maxWait","10000");
        p.setProperty("maxIdle","20");
        new BasicDataSourceFactory();
        DataSource ds = BasicDataSourceFactory.createDataSource(p);
        Environment evn = new Environment("mysql",new JdbcTransactionFactory(),ds);
        this.sf = new SqlSessionFactoryBuilder().build(new Configuration(evn));
        this.sf.getConfiguration().addMappers("com/fz/mapper");
//        sf.getConfiguration().addMapper(StudentMapper.class);
        this.session= this.sf.openSession();
        this.stu = this.session.getMapper(StudentMapper.class);

    }

    @After
    public void close(){
        this.session.commit();
        this.session.close();
    }
    @Test
    public void a(){
     List<Student> list = stu.query();
    }
}

